package io.gitee.devaper.starlight.common.bean

import io.gitee.devaper.starlight.common.entity.vo.*
import org.springframework.validation.BindException
import org.springframework.web.bind.MethodArgumentNotValidException
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.RestControllerAdvice

/**
 * WebMvc全局异常处理器
 */
@RestControllerAdvice
open class GlobalWebMvcExceptionHandler {

    /**
     * 处理WebMvc Validator产生的异常
     */
    @ExceptionHandler(value = [
        BindException::class,
        MethodArgumentNotValidException::class
    ])
    open fun bindExceptionHandler(exception: BindException): ApiResult<*> {
        // 将验证注解中的消息直接响应到前端
        return ApiResult.error(
            msg = exception.allErrors.first().defaultMessage!!
        )
    }

}